package gavdi.dk.view;



import java.util.ArrayList;
import java.util.Iterator;

import gavdi.dk.model.Event;
import gavdi.dk.view.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CustomHoursActivity extends Activity implements OnClickListener {
	
	private Bundle bundle;
	
	private TextView txtTime;
	private String hours;
	private String minutes;
	
	private Button btnPressed;
	private Button btnOK;

	
	private Button btnHour1;
	private Button btnHour2;
	private Button btnHour3;
	private Button btnHour4;
	private Button btnHour5;
	private Button btnHour6;
	private Button btnHour7;
	private Button btnHour8;
	private Button btnHour9;
	private Button btnHour10;
	private Button btnHour11;
	private Button btnHour12;
	private Button btnHour13;
	private Button btnHour14;
	private Button btnHour15;
	private Button btnHour16;
	private Button btnHour17;
	private Button btnHour18;
	private Button btnHour19;
	private Button btnHour20;
	private Button btnHour21;
	private Button btnHour22;
	private Button btnHour23;
	private Button btnHour24;
	
	private Button btnMin00;
	private Button btnMin15;
	private Button btnMin30;
	private Button btnMin45;
	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_hours_layout);
        
        initAttributes();
        setListener();
        
        btnOK.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				bundle = getIntent().getExtras();
				
				
			//	bundle.putString("hours", hours);
			//	bundle.putString("minutes", minutes);
				int newhours = ( Integer.parseInt(hours) * 60 + Integer.parseInt(minutes) );
				bundle.putString("hours", "" + newhours);
				
				Intent intent = getIntent().putExtras(bundle);
				
	            setResult(RESULT_OK, intent);
				finish();
				
			}
		});
        
        

    }

    private void initAttributes() {

    	txtTime = (TextView) findViewById(R.id.txtTime);
    	hours = "00";
    	minutes = "00";
    	
    	btnOK = (Button) findViewById(R.id.btnOKsendCustomTime);
    	
    	btnHour1 = (Button) findViewById(R.id.btnHour1);
    	btnHour2 = (Button) findViewById(R.id.btnHour2);
    	btnHour3 = (Button) findViewById(R.id.btnHour3);
    	btnHour4 = (Button) findViewById(R.id.btnHour4);
    	btnHour5 = (Button) findViewById(R.id.btnHour5);
    	btnHour6 = (Button) findViewById(R.id.btnHour6);
    	btnHour7 = (Button) findViewById(R.id.btnHour7);
    	btnHour8 = (Button) findViewById(R.id.btnHour8);
    	btnHour9 = (Button) findViewById(R.id.btnHour9);
    	btnHour10 = (Button) findViewById(R.id.btnHour10);
    	btnHour11 = (Button) findViewById(R.id.btnHour11);
    	btnHour12 = (Button) findViewById(R.id.btnHour12);
    	btnHour13 = (Button) findViewById(R.id.btnHour13);
    	btnHour14 = (Button) findViewById(R.id.btnHour14);
    	btnHour15 = (Button) findViewById(R.id.btnHour15);
    	btnHour16 = (Button) findViewById(R.id.btnHour16);
    	btnHour17 = (Button) findViewById(R.id.btnHour17);
    	btnHour18 = (Button) findViewById(R.id.btnHour18);
    	btnHour19 = (Button) findViewById(R.id.btnHour19);
    	btnHour20 = (Button) findViewById(R.id.btnHour20);
    	btnHour21 = (Button) findViewById(R.id.btnHour21);
    	btnHour22 = (Button) findViewById(R.id.btnHour22);
    	btnHour23 = (Button) findViewById(R.id.btnHour23);
    	btnHour24 = (Button) findViewById(R.id.btnHour24);
    	
    	btnHour1.setTag("hours");
    	btnHour2.setTag("hours");
    	btnHour3.setTag("hours");
    	btnHour4.setTag("hours");
    	btnHour5.setTag("hours");
    	btnHour6.setTag("hours");
    	btnHour7.setTag("hours");
    	btnHour8.setTag("hours");
    	btnHour9.setTag("hours");
    	btnHour10.setTag("hours");
    	btnHour11.setTag("hours");
    	btnHour12.setTag("hours");
    	btnHour13.setTag("hours");
    	btnHour14.setTag("hours");
    	btnHour15.setTag("hours");
    	btnHour16.setTag("hours");
    	btnHour17.setTag("hours");
    	btnHour18.setTag("hours");
    	btnHour19.setTag("hours");
    	btnHour20.setTag("hours");
    	btnHour21.setTag("hours");
    	btnHour22.setTag("hours");
    	btnHour23.setTag("hours");
    	btnHour24.setTag("hours");
    	
    	btnMin00 = (Button) findViewById(R.id.btnMin00);
    	btnMin15 = (Button) findViewById(R.id.btnMin15);
    	btnMin30 = (Button) findViewById(R.id.btnMin30);
    	btnMin45 = (Button) findViewById(R.id.btnMin45);
    	
    	btnMin00.setTag("minutes");
    	btnMin15.setTag("minutes");
    	btnMin30.setTag("minutes");
    	btnMin45.setTag("minutes");

	}
    
    private void setListener() {
    	btnHour1.setOnClickListener(this);
    	btnHour2.setOnClickListener(this);
    	btnHour3.setOnClickListener(this);
    	btnHour4.setOnClickListener(this);
    	btnHour5.setOnClickListener(this);
    	btnHour6.setOnClickListener(this);
    	btnHour7.setOnClickListener(this);
    	btnHour8.setOnClickListener(this);
    	btnHour9.setOnClickListener(this);
    	btnHour10.setOnClickListener(this);
    	btnHour11.setOnClickListener(this);
    	btnHour12.setOnClickListener(this);
    	btnHour13.setOnClickListener(this);
    	btnHour14.setOnClickListener(this);
    	btnHour15.setOnClickListener(this);
    	btnHour16.setOnClickListener(this);
    	btnHour17.setOnClickListener(this);
    	btnHour18.setOnClickListener(this);
    	btnHour19.setOnClickListener(this);
    	btnHour20.setOnClickListener(this);
    	btnHour21.setOnClickListener(this);
    	btnHour22.setOnClickListener(this);
    	btnHour23.setOnClickListener(this);
    	btnHour24.setOnClickListener(this);
    	
    	btnMin00.setOnClickListener(this);
    	btnMin15.setOnClickListener(this);
    	btnMin30.setOnClickListener(this);
    	btnMin45.setOnClickListener(this);

	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.custom_hours_layout, menu);
        return true;
    }

	public void onClick(View v) {

			if ( v.getTag() == "hours" ) 
			{
				btnPressed = (Button)v;
				hours = btnPressed.getText().toString();
				
				txtTime.setText(hours + ":" + minutes);
			}
			else if ( v.getTag() == "minutes")
			{
				btnPressed = (Button)v;
				minutes = btnPressed.getText().toString();
				
				txtTime.setText(hours + ":" + minutes);
			}
		}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// --- save whatever you need to persist---


		
		outState.putString("hours", hours);
		outState.putString("minutes", minutes);





		
		
		
		super.onSaveInstanceState(outState);
		
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// ---retrieve the information persisted earlier---

		super.onRestoreInstanceState(savedInstanceState);

		hours = savedInstanceState.getString("hours");
		minutes = savedInstanceState.getString("minutes");
		txtTime.setText(hours + ":" + minutes);

		
	}

	
		
		
		
	}

